﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;

namespace Rose.Helper
{
    public class ArticleCommentHelpModel
    {
        private  Database_RoseEntities db = new Database_RoseEntities();


        #region  实现类型转换
        public   CommentModel ChangeCommentToCommentModel(ArticleComment comment)
        {
            string userId = comment.UserId;
            UserInfo userInfo = new CheckHelpModel().CheckUserInfo(userId);
            return new CommentModel()
            {
                ImagePath = userInfo.ImagePath,
                Content = HttpContext.Current.Server.HtmlEncode(comment.Content),
                CreateTime = comment.CreateTime.ToString(),
                Id = comment.Id.ToString(),
                UserId = userId,
                Name=userInfo.Name
            };
        }

        private  List<CommentModel> ChangeCommentsToCommentModels(List<ArticleComment> comments)
        {
            List<CommentModel> returnModel = new List<CommentModel>();
            foreach (var comment in comments)
            {
                returnModel.Add(ChangeCommentToCommentModel(comment));
            }
            return returnModel;
        }
        #endregion


        #region  或者首个 最后一个
        private  CommentModel GetFirstCommentModel(Guid articleId)
        {
            return ChangeCommentToCommentModel(db.ArticleComments.Where(c => c.ArticleId == articleId).OrderBy(c => c.CreateTime).First());
        }

        private  CommentModel GetLastCommentModel(Guid articleId)
        {
            return ChangeCommentToCommentModel(db.ArticleComments.Where(c => c.ArticleId == articleId).OrderByDescending(c => c.CreateTime).First());
        }

        public  CommentModel GetFirstCommentModel(string articleId)
        {
            return GetFirstCommentModel(new Guid(articleId));
        }

        public  CommentModel GetLastCommentModel(string articleId)
        {
            return GetLastCommentModel(new Guid(articleId));
        }
        #endregion

        #region 获取列表


        private  List<ArticleComment> GetArticleCommentList(Guid articleId, bool isDescending)
        {
            if (isDescending)
                return db.ArticleComments.Where(c => c.ArticleId == articleId).OrderByDescending(c => c.CreateTime).ToList();
            else
                return db.ArticleComments.Where(c => c.ArticleId == articleId).OrderBy(c => c.CreateTime).ToList();
        }

        private  List<ArticleComment> GetArticleCommentList(string articleId, bool isDescending)
        {
            return GetArticleCommentList(new Guid(articleId), isDescending);
        }

        public  List<CommentModel> GetCommentModelList(string articleId, bool isDescending)
        {
            return ChangeCommentsToCommentModels(GetArticleCommentList(articleId, isDescending));
        }


        #endregion
    }
}